package com.niit.listener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

import com.niit.dao.LoginDao;
import com.niit.dao.impl.LoginDaoImpl;
import com.niit.utils.MD5Util;
import com.niit.view.LoginView;
import com.niit.view.MainView;


/**
 * 登录按钮的点击事件
 */
public class LoginListener extends MouseAdapter{
	private LoginView view;
	private LoginDao dao ;
	
	public LoginListener(LoginView loginView) {
		this.view = loginView;
		dao = new LoginDaoImpl();
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		String uname = view.getUserNameTextField().getText();
		String pwd = new String(view.getPwdTextField().getPassword());
		if(null != uname && !uname.trim().equals("")  ) {
			String pwMD5 = MD5Util.encrypt(pwd);
			boolean flag = dao.login(uname, pwMD5);
			if(flag) {
				new MainView(null);
				view.dispose();
			}else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误");
			}
		}else {
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
	}
}
